package com.yoooya.base.admin.controller.utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


@Controller
@RequestMapping(value = "/utils")
public class FileUploadController {

    @RequestMapping(value = "upload")
    @ResponseBody
    public Object upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> map = multipartRequest.getMultiFileMap();
        if (map != null) {
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
                String str = (String) iter.next();
                List<MultipartFile> fileList = map.get(str);
                for (MultipartFile mpf : fileList) {
                    File localFile = new File("c:\\temp\\" + StringUtils.trimAllWhitespace(mpf.getOriginalFilename()));
                    OutputStream out = new FileOutputStream(localFile);


                    out.write(mpf.getBytes());
                    out.close();
                }
            }
        }
        return new String() ;
    }

}